package com.lhb.controller;

import com.lhb.pojo.BaseResp;
import com.lhb.pojo.Course;
import com.lhb.pojo.rep.CourseReq;
import com.lhb.service.CourseService;
import com.lhb.utils.UploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.10.9 下午 19:10
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    @Autowired
    UploadUtils uploadUtils;

    @RequiresPermissions(value = {"COURSE_FINDALL"})
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public BaseResp findAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        BaseResp all = courseService.findAll(page, limit);
        return all;
    }

    //删除
    @RequestMapping(value = "del", method = RequestMethod.GET)
    public BaseResp delete(@RequestParam("id") Integer id) {
        int i = courseService.del(id);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(1);
        baseResp.setMsg("删除成功");
        return baseResp;
    }

    //新增
    @RequiresPermissions(value = {"COURSE_INSERT"})
    @RequestMapping("/insertCourse")
    public BaseResp insertCourse(@RequestBody CourseReq courseReq) {
        return courseService.insertCourse(courseReq);
    }

    //图片上传
    @RequestMapping("/upload")
    public BaseResp upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String upload = uploadUtils.upload(multipartFile);
        return new BaseResp(0, "上传成功！", null, upload);
    }

    @RequiresPermissions(value = {"COURSE_UPDATE"})
    @RequestMapping("/update")
    public BaseResp update(@RequestBody Course course) {
        return courseService.updateCourser(course);
    }
}
